package com.ts.proj.controller;

import com.alibaba.fastjson.JSONArray;
import com.ts.base.controller.BaseController;
import com.ts.common.constant.ConsoleConstant;
import com.ts.proj.entity.SystemMenuEntity;
import com.ts.proj.service.SystemMenuService;
import com.ts.util.JurisdictionUtil;
import com.ts.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class SystemMenuController extends BaseController {

    @Autowired
    private SystemMenuService systemMenuService;

    /**
     * 菜单列表
     * @return
     */
    @RequestMapping("/admin-menu-manage")
    public ModelAndView menuListManage(){
        ModelAndView mv = this.getModelAndView();
        String viewName = "login";
        if(!JurisdictionUtil.isValid()){
            mv.setViewName(viewName);
            return mv;
        }
        Object attribute = this.getRequest().getSession().getAttribute(ConsoleConstant.BTNRIGHTS);
        List<PageData> buttonList = (List<PageData>) JSONArray.parse(attribute.toString());
        mv.addObject("buttons", buttonList);
        mv.setViewName("admin/admin-menu-manage");
        return mv;
    }

    @RequestMapping("/admin-menu-add/{flag}/{id}")
    public ModelAndView menuAddManage(@PathVariable("flag") String flag, @PathVariable("id") String id){
        ModelAndView mv = this.getModelAndView();
        String viewName = "login";
        if(JurisdictionUtil.isValid()){
            PageData pd = this.getPageData();
            SystemMenuEntity systemMenuEntity = systemMenuService.findById(Integer.valueOf(id));
            //表示新增
            if(null!=pd.get("parentId")&&!"".equals(pd.getString("parentId"))){
                systemMenuEntity.setParentId(pd.getString("parentId"));
                systemMenuEntity.setId(-2);
            }
            viewName = "admin/admin-menu-add";
            if("son".equals(flag)){
                PageData temp = new PageData();
                temp.put("parentId", "0");
                List<SystemMenuEntity> all = systemMenuService.findAll(temp);
                mv.addObject("list", all);
            }
            mv.addObject("menu", systemMenuEntity);
            mv.addObject("flag", flag);
        }
        mv.setViewName(viewName);
        return mv;
    }






}
